package pro.softzhang.algo.lc300;

import java.util.LinkedList;

/**
 *
 */
public class LC394_DecodeString {
    public static void main(String[] args) {

    }

    /**
     *
     */
    static
    class Solution {
        public String decodeString(String s) {
            StringBuilder sb = new StringBuilder();
            int multi = 0;
            LinkedList<Integer> stack_multi = new LinkedList<>();
            LinkedList<String> stack_res = new LinkedList<>();
            for(Character c : s.toCharArray()) {
                if(c == '[') {
                    stack_multi.addLast(multi);
                    stack_res.addLast(sb.toString());
                    multi = 0;
                    sb = new StringBuilder();
                }
                else if(c == ']') {
                    StringBuilder tmp = new StringBuilder();
                    int cur_multi = stack_multi.removeLast();
                    for(int i = 0; i < cur_multi; i++) tmp.append(sb);
                    sb = new StringBuilder(stack_res.removeLast() + tmp);
                }
                else if(c >= '0' && c <= '9') multi = multi * 10 + Integer.parseInt(c + "");
                else sb.append(c);
            }
            return sb.toString();
        }
    }
}
